﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NhanAi.Entity;
using NhanAi.DAO;
using NhanAi.App_Code;
namespace NhanAi.Public
{
    public partial class Public : System.Web.UI.MasterPage
    {
        List<Post> viewMost;
        public List<Post> ViewMostPosts { get { return viewMost; } }
        public Post IntroPost{
            get
            {
                if (Application["intro"] != null) return Application["intro"] as Post;
                return null;
            }
    }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod != "POST")
            {
                if (Session["view_most"] != null)
                {
                    if (Session["curlang"] == Session["language"])
                    {
                        viewMost = (List<Post>)Session["view_most"];
                    }
                    else
                    {
                        PostDao dao = new PostDao();
                        string lang = "";
                        if (Session["language"] == null) lang = "vi";
                        else { lang = Session["language"].ToString(); }
                        viewMost = dao.ReadViewMost(lang == "vi" ? NAConstants.LANGUAGE_VI : NAConstants.LANGUAGE_EN, 5);
                        Session["view_most"] = viewMost;
                        Session["curlang"] = lang;
                    }
                }
                else
                {
                    PostDao dao = new PostDao();
                    string lang = "";
                    if (Session["language"] == null) lang = "vi";
                    else { lang = Session["language"].ToString(); }
                    viewMost = dao.ReadViewMost(lang == "vi" ? NAConstants.LANGUAGE_VI : NAConstants.LANGUAGE_EN, 5);
                    Session["view_most"] = viewMost;
                    //Session["curlang"] = lang;
                }
            }
            if (Request.HttpMethod == "POST" && Request["lang"]!=null)
            {
                string lang = Request["lang"] == null ? "vi" : Request["lang"];
                Session["language"] = lang;
                string url = Request.RawUrl;
                Response.Redirect(url);

            }
            if (Application["intro"] == null)
            {
                PostDao dao = new PostDao();
                Application["intro"] = dao.Read("doctor", "page", NAConstants.ALL_LANGUAGE);
            }
           
        }
        public List<object[]> GetSubmenuItems()
        {
            if (Application["submenu"] == null)
            {
                TaxonomyDao daoTax = new TaxonomyDao();
                string[] keys = { "tin-tuc", "y-li", "dong-duoc", "cham-cuu", "phuong-te", "benh-hoc", "dieu-tri", "tai-lieu","lop-hoc" };
                 List<object[]> list = new  List<object[]>();
                foreach (string k in keys)
                {
                    Taxonomy parent = daoTax.Read(k, NAConstants.ALL_LANGUAGE);
                    if (parent != null)
                    {
                        List<Taxonomy> childrens = daoTax.ReadAll(parent.Id);
                        object[] item=new object[]{k,parent,childrens};
                        list.Add(item);
                    }
                }
                Application["submenu"] = list;
                return list;
            }
            else
            {
                 List<object[]> list = Application["submenu"] as List<object[]>;
                return list;
            }
        }
        //public SortedList<Taxonomy,List<Taxonomy>> ProcessMenu()
        //{
        //    if (Application["submenu"] == null)
        //    {
        //        TaxonomyDao daoTax = new TaxonomyDao();
        //        string[] keys = { "tin-tuc", "y-li", "dong-duoc", "cham-cuu", "phuong-te", "benh-hoc", "dieu-tri", "tai-lieu", "lop-hoc" };
        //        SortedList<Taxonomy, List<Taxonomy>> list = new SortedList<Taxonomy, List<Taxonomy>>();
        //        foreach (string k in keys)
        //        {
        //            Taxonomy parent = daoTax.Read(k, NAConstants.ALL_LANGUAGE);
        //            if (parent != null)
        //            {
        //                List<Taxonomy> subitems = daoTax.ReadAll(parent.Id);
        //                list.Add(parent, subitems);
        //                Application["submenu"] = list;
        //            }
        //        }
        //        return list;
        //    }
        //    else
        //    {
        //        SortedList<Taxonomy, List<Taxonomy>> list = Application["submenu"] as SortedList<Taxonomy, List<Taxonomy>>;
        //        return list;
        //    }
        //}
        
    }
}